package db;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;

public class IngredientiDataSource {
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;

	private String[] ingredientiCol = { MySQLiteHelper.COLUMN_CHIAVE, MySQLiteHelper.COLUMN_NOME,
			MySQLiteHelper.COLUMN_IMMAGINE, MySQLiteHelper.COLUMN_CATEGORIA
	};

	public IngredientiDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Ingrediente createIngrediente(String nome, String immagine, String categoria) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_NOME, nome);
		//values.put(MySQLiteHelper.COLUMN_DESCRIZIONE, descrizione);
		values.put(MySQLiteHelper.COLUMN_IMMAGINE, immagine);
		values.put(MySQLiteHelper.COLUMN_CATEGORIA, categoria);
		long insertId = database.insert(MySQLiteHelper.TABLE_INGREDIENTE, null,
				values);
		Cursor cursor = database.query(MySQLiteHelper.TABLE_INGREDIENTE,
				ingredientiCol, MySQLiteHelper.COLUMN_CHIAVE + " = " + insertId, null,
				null, null, null);

		cursor.moveToFirst();
		Ingrediente nuovoIngrediente = cursorToIngrediente(cursor);
		cursor.close();
		return nuovoIngrediente;
	}

	public void deleteIngrediente(Ingrediente ingrediente) {
		long id = ingrediente.getId();
		System.out.println("Comment deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_RICETTA, MySQLiteHelper.COLUMN_CHIAVE
				+ " = " + id, null);
	}

	public List<Ingrediente> getAllIngredienti() {
		List<Ingrediente> ingredienti = new ArrayList<Ingrediente>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_INGREDIENTE,
				ingredientiCol, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Ingrediente ingrediente = cursorToIngrediente(cursor);
			ingredienti.add(ingrediente);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		return ingredienti;
	}

	private Ingrediente cursorToIngrediente(Cursor cursor) {
		Ingrediente ingrediente = new Ingrediente();
		ingrediente.setId(cursor.getLong(0));
		ingrediente.setNome(cursor.getString(1));
		ingrediente.setImmagine(cursor.getString(2));
		ingrediente.setCategoria(cursor.getString(3));
		return ingrediente;
	}
}
